package cn.springbak.publish.web.controller;

import cn.springbak.publish.Service.SysNoticeService;
import cn.springbak.publish.common.util.PageUtil;
import cn.springbak.publish.dao.SysNoticeDao;
import cn.springbak.publish.pojo.SysNotice;
import cn.springbak.publish.web.pojo.JsonResult;
import cn.springbak.publish.web.pojo.JsonResult;
import com.github.pagehelper.ISelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class SysNoticeController {

    @Autowired
    private SysNoticeService sysNoticeService;

    @GetMapping
    public JsonResult doFindNotices(SysNotice notice){

        return new JsonResult(PageUtil.startPage().doSelectPageInfo(
        new ISelect(){
            @Override
            public void doSelect() {
                sysNoticeService.findNotices(notice);
            }
        }));
    }

    @PostMapping
    public JsonResult doSaveNotice(@RequestBody SysNotice notice){
//        try {
        sysNoticeService.saveNotice(notice);
//        }catch(Throwable e){
//            return new JsonResult(e);
//        }
        return new JsonResult("save ok");
    }
    /**基于id查询notice对象*/
    //rest风格url(软件架构编码风格),允许在url中基于{}方式定义变量
    //rest风格应用目的:让url的设计更加通用和简单
    //访问url: http://ip:port/notice/doFindById/14
    //方法参数的值假如来自url中{var}变量的值,则需要使用@PathVariable注解对参数进行描述
    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable Long id){
        return  new JsonResult(sysNoticeService.findById(id));
    }

    @PutMapping
    public JsonResult doUpdateNotice(SysNotice notice){
        sysNoticeService.updateNotice(notice);
        return new JsonResult("update ok");
    }

    @DeleteMapping
    public JsonResult doDeleteById(@PathVariable  Long... ids){
        sysNoticeService.deleteById(ids);
        return new JsonResult("delete ok");
    }


}
